from datetime import datetime, date, time
from typing import Optional
from fastapi import APIRouter, Query
from apps.schemas.schemas import ScheduleCreateRequest, ScheduleUpdateRequest
from apps.services.schedules import (
    get_all_schedules,
    get_schedules_by_coach,
    get_schedules_by_coach_and_day,
    create_schedule,
    update_schedule,
    delete_schedule
)

schedules = APIRouter()


@schedules.get("/msg")
async def get_all_schedules_api(page: int = Query(1, ge=1), page_size: int = Query(10, ge=1, le=100)):
    """
    获取所有教练排班列表（分页）
    """
    try:
        total, schedules_data = await get_all_schedules(page, page_size)
        return {
            "status": 200,
            "message": "success",
            "data": schedules_data,
            "total": total
        }
    except Exception as e:
        return {
            "status": 500,
            "message": f"Error retrieving schedules: {str(e)}",
            "data": [],
            "total": 0
        }


@schedules.get("/msg/{coach_id}")
async def get_schedule_api(coach_id: int):
    """
    根据教练ID获取排班信息
    """
    try:
        schedules_data = await get_schedules_by_coach(coach_id)
        if schedules_data:
            return {
                "status": 200,
                "message": "success",
                "data": schedules_data
            }
        else:
            return {
                "status": 404,
                "message": "Schedule not found",
                "data": []
            }
    except Exception as e:
        return {
            "status": 500,
            "message": f"Error retrieving schedule: {str(e)}",
            "data": []
        }


@schedules.get("/msg/{coach_id}/{day_of_week}")
async def get_schedule_by_coach_and_day_api(coach_id: int, day_of_week: str):
    """
    根据教练ID和星期几获取排班信息
    """
    try:
        schedules_data = await get_schedules_by_coach_and_day(coach_id, day_of_week)
        if schedules_data:
            return {
                "status": 200,
                "message": "success",
                "data": schedules_data
            }
        else:
            return {
                "status": 404,
                "message": "Schedule not found",
                "data": []
            }
    except Exception as e:
        return {
            "status": 500,
            "message": f"Error retrieving schedule: {str(e)}",
            "data": []
        }


@schedules.post("/add")
async def create_schedule_api(data: ScheduleCreateRequest):
    """
    创建新的教练排班
    """
    try:
        await create_schedule(data)
        return {
            "status": 201,
            "message": "Schedule created successfully",
            "data": {}
        }
    except ValueError as e:
        return {
            "status": 404,
            "message": str(e),
            "data": {}
        }
    except Exception as e:
        return {
            "status": 500,
            "message": f"Error creating schedule: {str(e)}",
            "data": {}
        }


@schedules.put("/update/{schedule_id}")
async def update_schedule_api(schedule_id: int, data: ScheduleUpdateRequest):
    """
    更新教练排班
    """
    try:
        updated = await update_schedule(schedule_id, data)
        if updated:
            return {
                "status": 200,
                "message": "Schedule updated successfully",
                "data": {}
            }
        else:
            return {
                "status": 404,
                "message": "Schedule not found",
                "data": {}
            }
    except ValueError as e:
        return {
            "status": 404,
            "message": str(e),
            "data": {}
        }
    except Exception as e:
        return {
            "status": 500,
            "message": f"Error updating schedule: {str(e)}",
            "data": {}
        }


@schedules.delete("/delete/{schedule_id}")
async def delete_schedule_api(schedule_id: int):
    """
    删除教练排班
    """
    try:
        deleted = await delete_schedule(schedule_id)
        if deleted:
            return {
                "status": 200,
                "message": "Schedule deleted successfully",
                "data": {}
            }
        else:
            return {
                "status": 404,
                "message": "Schedule not found",
                "data": {}
            }
    except Exception as e:
        return {
            "status": 500,
            "message": f"Error deleting schedule: {str(e)}",
            "data": {}
        }
